package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.MoreNovelShowAdapter;
import com.taptech.doufu.bean.MoreNovelCategoryBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.HomeMainServices;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longsiyang on 16/5/19.
 */
public class MoreNovelShowActivity extends DiaobaoBaseActivity implements HttpResponseListener, View.OnClickListener,PullToRefreshListView.OnLoadAndRefreshListener
{
    private PullToRefreshListView listview;
    private MoreNovelShowAdapter ada;
    private String last="";
    private List<MoreNovelCategoryBean> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dfmorenovelshow);
        initsView();
        getDataFromNet();
    }

    private void initsView(){
        listview=(PullToRefreshListView)findViewById(R.id.listview_morenovel_list);
        listview.setRefreshable(true);
        listview.setOnLoadAndRefreshListener(this);
    }

    private void getDataFromNet(){
        HomeMainServices.getInstance().getHomeNovelMoreCategory(this, last);
    }

    @Override
    public void onRefresh()
    {
        last="";
        getDataFromNet();
    }

    @Override
    public void loadMore()
    {
        getDataFromNet();
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId()){
            case R.id.back_morenovelshow:
                MoreNovelShowActivity.this.finish();
                break;
        }
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        //加载成功
        if(Constant.STATUS_SUCCESS==response.getStatus()){
            switch (handleType){
                case HomeMainServices.HANDLE_TYPE_HOMENOVEL_NOVELCATEGORY_MORE:
                    JSONObject objectData=(JSONObject)response.getData();
                    try{
                        if(""==last){//来的是第一页的数据
                            listData=new ArrayList<MoreNovelCategoryBean>();
                            listData=com.alibaba.fastjson.JSONArray.parseArray(objectData.getJSONArray("list").toString(),MoreNovelCategoryBean.class);
                            ada=new MoreNovelShowAdapter(MoreNovelShowActivity.this,listData);
                            listview.setAdapter(ada);
                        }else{//来的是第二页的数据
                            List<MoreNovelCategoryBean> tempList=com.alibaba.fastjson.JSONArray.parseArray(objectData.getJSONArray("list").toString(), MoreNovelCategoryBean.class);
                            listData.addAll(tempList);
                            ada.notifyDataSetChanged();
                        }
                        String templast = ((JSONObject) response.getData()).getString("last");
                        if((templast!=null)&&(!templast.equals(last))){
                            last=templast;
                            listview.onRefreshComplete();
                            listview.setLoadmoreable(true);
                        }else{
                            //结束ListView加载更多
                            listview.getLoadCompleteImg().setImageResource(R.drawable.draw_recommand_bottom_img);
                            listview.setFull(true);
                            listview.loadMoreComplete();
                            listview.setLoadmoreable(false);
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
}
